package com.liu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.liu.action.PersonAction;
import com.liu.model.FromPerson;
import com.liu.model.Person;
import com.liu.model.Test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//把这个class定义为一个Controller类
@Controller
public class HelloController {

	PersonAction action = new PersonAction();
	private static final Log logger = LogFactory.getLog(HelloController.class);

	// 使用@RequestMapping标注来把Person和这个Controller关联
	@RequestMapping("/Person.do")
	public ModelAndView Person() throws Exception {
		List<Person> list = action.query("%");
		logger.info("1!list=====>");
		// 返回到message.jsp的strx标识list列表
		return new ModelAndView("message", "strx", list);
	}

	@RequestMapping("/Delete.do")
	@ResponseBody
	public ModelAndView Delete(HttpServletRequest request) throws Exception {
		Integer i = Integer.valueOf(request.getParameter("id"));
		action.dlt(i);
		List<Person> list = action.query("%");
		return new ModelAndView("message", "strx", list);
	}

	@RequestMapping(value = "/Find.do")
	public @ResponseBody String Find(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("1!fid=====>" + request.getParameter("fid"));
		Integer id = Integer.valueOf(request.getParameter("fid"));
		String p = action.find(id).toString(id);
		logger.info("2!p====>" + p);
		logger.info("3!response========>" + response.getWriter());
		response.getWriter().print(p);
		return null;
	}

	@RequestMapping(value = "/Json.do", method = RequestMethod.POST)
	@ResponseBody
	public Person saveUser(Person p) throws Exception {
		logger.info("1!person.get(0)======>" + p.getUser_name());
		logger.info("2!");
		p.setUser_name(p.getUser_name());
		p.setSex(p.getSex());
		p.setAge(p.getAge());
		p.setAddr(p.getAddr());
		action.add(p);
		logger.info("1!action.findid()======>" + action.findid());
		p.setId(action.findid());
		return p;
	}

	@RequestMapping(value = "/page.do")
	public ModelAndView page(HttpServletRequest request) throws Exception {
		logger.info("1!page=====>" + request.getParameter("pageid"));
		Integer pageid = Integer.valueOf(request.getParameter("pageid"));
		List<Person> list = action.showPersonByPage((pageid-1)*2,2);
		// 返回到message.jsp的strx标识list列表
		return new ModelAndView("message", "strx", list);
	}
	
	@RequestMapping(value = "/pagecount.do")
	@ResponseBody
	public String pagecount(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int personc = action.personCount();
		int pagec = 0;
		if(personc%2==0){
			pagec = personc/2;
		}
		else{
			pagec = personc/2+1;
		}
		logger.info("1!pc====>" + pagec);
		//response.print(pagec);
		return String.valueOf(pagec);
	}

}
